import java.io.File;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;

import org.junit.Test;



public class ReferenceObject {
    
    private PhantomReference<File> imagePhantom;
    private WeakReference<File> imageWeak;
    private SoftReference<File> imageSoft;
    private File imageStrong;
    
    public ReferenceObject(){
        imageStrong = new File("E://test.jpg");
        ReferenceQueue<File> deadFile = new ReferenceQueue<File>(); 
        imagePhantom = new PhantomReference<File>(imageStrong,deadFile);
        imageWeak = new WeakReference<File>(imageStrong);
        imageSoft = new SoftReference<File>(imageStrong);
    }
    
//    @BeforeClass
//    public void setNullReference(){
//        
//    }
    
//    @AfterClass
//    public void finallyClass(){
//        imageStrong = null;
//    }
    
    @Test
    public void test(){
        
//        imageStrong = null;
//        System.gc();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(imageStrong != null){
            System.out.println("StrongReference after object null : " + imageStrong.getName());
        }
        
        if(imagePhantom.get() != null){
            System.out.println("PhantomReference after object null : " + imagePhantom.get().getName());
        }
        
        if(imageWeak.get() != null){
            System.out.println("WeakReference after object null : " + imageWeak.get().getName());
        }
        
        if(imageSoft.get() != null){
            System.out.println("SoftReference after object null : " + imageSoft.get().getName());
        }
       
    }
}
